package biz.datalk.industrialland.common.util;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author tarofang@163.com
 * @date 2021-03-30
 */
public class UrlEncodeUtil {
    private static final Logger log = LoggerFactory.getLogger(UrlEncodeUtil.class);

    public static String urlEncode(String str) {
        // try {
        //     if (StringUtils.isBlank(str)) {
        //         return str;
        //     }
        //     String encode = URLEncoder.encode(str, StandardCharsets.UTF_8.name());
        //     encode = encode.replaceAll("\\+", "%20"); // 空格会转为+, 这里做个修正
        //     return encode;
        // } catch (Exception ex) {
        //     log.warn("URLEncode fail: {}", ex.getMessage());
        //     return str;
        // }

        if (StringUtils.isBlank(str)) {
            return str;
        }
        LinkedHashMap<String, String> replaceMap = Maps.newLinkedHashMap();
        replaceMap.put("+", "%20");
        return urlEncode(str, StandardCharsets.UTF_8, replaceMap);
    }

    public static String urlEncode(String str, Charset charsets, LinkedHashMap<String, String> replaceMap) {
        try {
            if (StringUtils.isBlank(str)) {
                return str;
            }
            String encode = URLEncoder.encode(str, charsets.name());
            // encode = encode.replaceAll("\\+", "%20"); // 空格会转为+, 这里做个修正
            if (replaceMap != null && !replaceMap.isEmpty()) {
                for (Map.Entry<String, String> entry : replaceMap.entrySet()) {
                    encode = StringUtils.replace(encode, entry.getKey(), entry.getValue());
                }
            }
            return encode;
        } catch (Exception ex) {
            log.warn("URLEncode fail: {}", ex.getMessage());
            return str;
        }
    }

    public static String urlDecode(String str) {
        // try {
        //     if (StringUtils.isBlank(str)) {
        //         return str;
        //     }
        //     return URLDecoder.decode(str, StandardCharsets.UTF_8.name());
        // } catch (Exception ex) {
        //     log.warn("URLDecoder fail: {}", ex.getMessage());
        //     return str;
        // }

        if (StringUtils.isBlank(str)) {
            return str;
        }
        return urlDecode(str, StandardCharsets.UTF_8);
    }

    public static String urlDecode(String str, Charset charsets) {
        try {
            if (StringUtils.isBlank(str)) {
                return str;
            }
            return URLDecoder.decode(str, charsets.name());
        } catch (Exception ex) {
            log.warn("URLDecoder fail: {}", ex.getMessage());
            return str;
        }
    }

    public static void main(String[] args) {
        String str = "112233中文 +H+h *敏感词-x(/).xlsx";
        String s = urlEncode(str);
        System.out.println("s = " + s);
        String sx = urlDecode(s);
        System.out.println("sx = " + sx);
    }
}
